package com.yonyougov.cruxtestcase.testcase.user;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yonyougov.cruxtestcase.component.CommonLogin;
import com.yonyougov.cruxtestcase.component.ZWCDInvoke;
import com.yonyougov.cruxtestcase.consts.Headers;
import com.yonyougov.cruxtestcase.dto.BaseResponseDto;
import com.yonyougov.cruxtestcase.testcase.user.dto.FunctionGroupInDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class FunctionGroupService {
    @Autowired
    private  ObjectMapper objectMapper;
    @Autowired
    private ZWCDInvoke zwcdInvoke;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private CommonLogin commonLogin;

    private String queryUserOrgUrl="/crux-permission/api/functiongroup/functiongroupbyorg?orgId=";

    public BaseResponseDto caseOne(FunctionGroupInDto baseDto) {
        BaseResponseDto baseResponseDto = new BaseResponseDto();
        String baseUrl = zwcdInvoke.getBaseUrl(baseDto.getEnvName());
        if (!StringUtils.isEmpty(baseUrl)) {
            String token = commonLogin.getToken(baseUrl, baseDto.getUser(), baseDto.getPassword());
            if (StringUtils.isNotBlank(token)) {
                HttpHeaders headers=new HttpHeaders();
                headers.add(Headers.HEADER_TOKEN,Headers.AUTH2_PREFIX+token);
                ResponseEntity response = restTemplate.exchange(baseUrl+queryUserOrgUrl+baseDto.getOrgId(), HttpMethod.GET, new HttpEntity<>(headers),
                        Object.class);

                if (response.getStatusCode() == HttpStatus.OK && response.hasBody()) {
                    //获取到response body后的处理逻辑
                    this.postProcessResponse(response,baseResponseDto);
                }else {
                    baseResponseDto.setSuccess(false);
                    baseResponseDto.setErrorMsg("业务接口请求失败");
                }
            }else {
                baseResponseDto.setSuccess(false);
                baseResponseDto.setErrorMsg("用户名或者密码错误，获取token失败");
            }
        }else {
            baseResponseDto.setSuccess(false);
            baseResponseDto.setErrorMsg("环境不存在");
        }
        return baseResponseDto;
    }

    /**
     * 获取到response body后的处理逻辑
     * @param response
     * @param baseResponseDto
     */
    public void postProcessResponse(ResponseEntity response,BaseResponseDto baseResponseDto){
        Object responseDto = response.getBody();
        if (responseDto == null) {
            baseResponseDto.setSuccess(false);
            baseResponseDto.setErrorMsg("组织下没有角色数据");
        }else{
            baseResponseDto.setSuccess(true);
            try {
                baseResponseDto.setData("functionGroupDto",
                        objectMapper.readValue(objectMapper.writeValueAsString(response.getBody()), Object.class));
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
        }
    }

}
